/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import id.duapuluhdua.dakidaki.gps.GpsReader;

/**
 *
 */
public class Preferences
{
  /** Constants */
  private static final String TAG = "Preferences";

  public static final int MEASURE_MODE_USA = 0;
  public static final int MEASURE_MODE_EU = 1;

  public static final int DEFAULT_UPDATE_INTERVAL = 15;  // in seconds
  public static final int DEFAULT_LOCATION_ACCURACY = (int)GpsReader.MINIMUM_ACCURACY;

  /** Fields */
  private Context mContext;
  private SharedPreferences mPrefs;

  public int measureMode;
  public int locationUpdateInterval;
  public int locationAccuracy;

  public boolean bLocationEditDecimal;

  private static ArrayList<Integer> vecIntervalOption = new ArrayList<Integer>();
  private static ArrayList<Integer> vecAccuracyOption = new ArrayList<Integer>();

  static
  {
    // Init update interval options
    vecIntervalOption.add(0);
    vecIntervalOption.add(2);
    vecIntervalOption.add(5);
    vecIntervalOption.add(10);
    vecIntervalOption.add(15);
    vecIntervalOption.add(30);
    vecIntervalOption.add(45);
    vecIntervalOption.add(60);

    // Init location accuracy options
    vecAccuracyOption.add(10);
    vecAccuracyOption.add(20);
    vecAccuracyOption.add(50);
    vecAccuracyOption.add(100);
    vecAccuracyOption.add(200);
  };

  /**
   * Default constructor.
   *
   * @param context the application context.
   */
  public Preferences(Context context)
  {
    mContext = context;
    load();
  }

  /**
   * Load the application settings.
   */
  public void load()
  {
    mPrefs = mContext.getSharedPreferences(TAG, Context.MODE_WORLD_READABLE);

    measureMode = mPrefs.getInt("measureMode", MEASURE_MODE_EU);
    locationUpdateInterval = mPrefs.getInt("locationUpdateInterval", DEFAULT_UPDATE_INTERVAL);
    locationAccuracy = mPrefs.getInt("locationAccuracy", DEFAULT_LOCATION_ACCURACY);

    bLocationEditDecimal = mPrefs.getBoolean("locationEditDecimal", true);
  }

  /**
   * Save the application settings. Returns true if the saving process runs
   * smoothly.
   *
   * @return true if no exception has occurred.
   */
  public boolean save()
  {
    try {
      SharedPreferences.Editor editor = mPrefs.edit();
      editor.putInt("measureMode", measureMode);
      editor.putInt("locationUpdateInterval", locationUpdateInterval);
      editor.putInt("locationAccuracy", locationAccuracy);
      editor.putBoolean("locationEditDecimal", bLocationEditDecimal);
      editor.commit();
      return true;
    }
    catch (Exception e) {
      Log.e(TAG, e.getMessage());
      return false;
    }
  }

  /**
   * Returns the current measure mode (i.e., US or EU).
   *
   * @return the measure mode.
   * @see MEASURE_MODE_USA
   * @see MEASURE_MODE_EU
   */
  public int getMeasureMode() {
    return measureMode;
  }

  /**
   * Returns the current update interval.
   *
   * @return the interval (in seconds).
   */
  public int getLocationUpdateInterval() {
    return locationUpdateInterval;
  }

  /**
   * Returns the index of the current interval setting.
   *
   * @return the option index.
   */
  public int getIntervalOptionIndex()
  {
    return vecIntervalOption.indexOf(new Integer(locationUpdateInterval));
  }

  /**
   * Updates the interval setting.
   *
   * @param index the option index.
   */
  public void setUpdatePeriodItemIndex(int index)
  {
    locationUpdateInterval = vecIntervalOption.get(index);
  }

  /**
   * Returns the current location accuracy threshold.
   *
   * @return the accuracy threshold.
   */
  public int getLocationAccuracy() {
    return locationAccuracy;
  }

  /**
   * Returns the index of the current accuracy setting.
   *
   * @return the option index.
   */
  public int getLocationAccuracyIndex()
  {
    return vecAccuracyOption.indexOf(new Integer(locationAccuracy));
  }

  /**
   * Updates the accuracy setting.
   *
   * @param index the option index.
   */
  public void setLocationAccuracyIndex(int index)
  {
    locationAccuracy = vecAccuracyOption.get(index);
  }
}
